/*
 * 通用上传js类
 * @memberOf {TypeName} 
 * @return {TypeName} 
 */
function SfaUpload() {
	var that  = this;
	
	var isUploading = false;
	
	/*
	 * 取当前是否正在上传的状态
	 * @return {TypeName} 
	 */
	this.getIsUploading = function(){
		return isUploading;
	}
	
	
	/*
	 * 初始化
	 * @param {Object} actionPath 上传的action类
	 * @param {Object} div  要显示的div层
	 */
	this.init = function(actionPath, div,i) {
		if(i==undefined) i='';
		var str = '<div id=\"addFileLoading'+i+'\" style=\"display: none;\">';
		str = str + '<div id=\"waitAddPic\" class=\"AddPicLoading\" style=\"float: left;padding-left: 16px;\">';
		str = str + '文件上传中,请稍候...';
		str = str + '</div>'
		str = str + '<a href=\"javascript:void(0)\" onclick=\"sfaUpload.delUploadShowAdd(this)\">&nbsp;<font color="red">删除</font></a></div>';
		str = str + '<div id=\"addFile'+i+'\">';
		str = str + '<form id=\"uploadForm'+i+'\" action=\"' + actionPath + '\" method=\"post\" enctype=\"multipart/form-data\">';
		str = str + '<span id=\"addFileSpan'+i+'\">';
		str = str + '<input type=\"file\" class=\"uploadFileInput\" id=\"AFFIX\" name=\"AFFIX\" onchange=\"sfaUpload.upload(\''+i+'\')\" \/>';
		str = str + '</span>';
		str = str + '<a href=\"javascript:void(0)\">添加附件<\/a> <font color="red" style="cursor: default;">（不能超过10M）</font>';
		str = str + '</form></div> ';
		$("#" + div).html(str);
	}
	
	/*
	 * 文件上传
	 * @return {TypeName} 
	 */
	this.upload = function(i) {
		var str = "";
		if ($(".uoloadedFile"+i+"[isDel='n']").length >= 5) {
			alert("最多只能上传5个附件");
			return false;
		}
		isUploading = true;
		//显示正在上传
		$("#addFileLoading"+i).show();
		$("#addFile"+i).hide();

		//ajax上传
		$("#uploadForm"+i).ajaxSubmit(function(result) {
			result = $("<span>" + result + "</span>").text();
			result = decodeURI(result, "utf-8");
			var obj = eval('(' + result + ')');
			if (obj.result) {
				var div = $("<div class=\"uoloadedFile"+i+"\" isDel=\"n\"></div>");
				div.append($("<span></span>").html(decodeURI(obj.message, "utf-8")));
				div.append($("<a href=\"javascript:void(0)\" onclick=\"sfaUpload.delUpload(this)\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;删除</a>"));
				div.attr("SALEAFFIXITEMID", obj.saleaffixitemid);
				$("#fileList"+i).append(div);
			} else {
				art.dialog( {
					icon : 'error',
					lock : true,
					resize : false,
					title : "上传失败",
					content : "上传失败<br />错误消息:" + obj.message
				});
				isUploading = false;
			}
			isUploading = false;
			$("#addFileLoading"+i).hide();
			$("#addFileSpan"+i).html("<input type=\"file\" class=\"uploadFileInput\" id=\"AFFIX\" name=\"AFFIX\" onchange=\"sfaUpload.upload(\'"+i+"\')\" />");
			$("#addFile"+i).show();
		});
	}
	
	/*
	 * 文件删除
	 * @param {Object} obj
	 */
	this.delUpload = function(obj) {
		$(obj).parent().attr("isDel", "y");
		$(obj).parent().css("display", "none");
	}
	
	
	/*
	 * 取消上传
	 * @param {Object} obj
	 */
	this.delUploadShowAdd= function(obj) {
		$(obj).parent().attr("isDel", "y");
		$(obj).parent().css("display", "none");
		$("#addFile").show();
		isUploading = false;
	}
}
var sfaUpload = new SfaUpload();

